﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Nomad.WebSync.Pocket.Sync {
    abstract class SynchronizationControl: IDisposable {
        /// <summary>
        /// Service data source instance
        /// </summary>
        protected WebSyncService.Mobile Service;
        /// <summary>
        /// Pocket Outlook session instace
        /// </summary>
        protected PocketOutlook.Application Session;

        /// <summary>
        /// Event to get progress changes on current synchronization
        /// </summary>
        public event ProgressEventHandler ProgressChanged;

        /// <summary>
        /// Creates a new instance of SynchronizationControl class
        /// </summary>
        public SynchronizationControl() {
            // Create a service instace
            this.Service = Services.Mobile.Connect();

            // Create a Pocket Outlook session
            this.Session = new PocketOutlook.ApplicationClass();
            this.Session.Logon(-1);
        }

        /// <summary>
        /// Dispose this instance of Contacts class
        /// </summary>
        public void Dispose() {
            // Ends the current pocket outlook session
            try { this.Session.Logoff(); } catch { }

            // Close current service connection
            try { this.Service.Dispose(); } catch { }
        }

        /// <summary>
        /// Synchronization starts process
        /// </summary>
        public abstract void Start();

        /// <summary>
        /// Dispare the event for current synchronization progress
        /// </summary>
        /// <param name="current">Current value for synchronization</param>
        /// <param name="total">Total value for synchronization</param>
        protected void DoProgressChanged(int current, int total) {
            if (this.ProgressChanged != null) {
                var args = new ProgressEventArgs() {
                    Current = current,
                    Maximum = total
                };

                this.ProgressChanged(this, args);
                Application.DoEvents();
            }
        }
    }
}
